package com.example.java.sort;

import java.util.Arrays;

public class InsertSort {
	public static void main(String[] args) {
	    int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};
	    insertSort(a);
	   System.out.println(Arrays.toString(a));
	}
	
	public static void insertSort(int [] a){
		int insertNum;
		for (int i = 0; i < a.length; i++) {
			insertNum =a[i];//插入值，前面都是有序的
			int j=i-1;
			while(j>=0 && a[j]>insertNum){//有序字段从后向前
				a[j+1]=a[j];//大于插入值，后移
				j--;//坐标左移,继续判断,直到a[j]<=insert值
			}
			a[j+1]= insertNum; //a[j]<=insert值,j+1位置插入
			
		}
		
	}
}
